package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {
    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private Executor threadPoolTaskExecutor;
    @Autowired
    private RedissonClient redissonClient;


    @Override
    public Map<String, Object> getItemData(Long albumId) {
        //判断访问的专辑在布隆过滤器中是否存在
        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        boolean flag = bloomFilter.contains(albumId);
        //如果返回true，说明访问的专辑可能存在
        //如果返回false，说明访问的专辑不存在，直接返回null
        if(!flag){
            throw new GuiguException(404, "专辑不存在！");
        }
        // 创建封装专辑详情相关数据
        Map<String, Object> map = new ConcurrentHashMap<>();

        //远程调用专辑服务获取专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑信息, {}不存在", albumId);
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolTaskExecutor).exceptionally(e->{
            log.error("远程调用专辑服务获取专辑信息失败！", e);
            return null;
        }).orTimeout(3, TimeUnit.SECONDS);


        // 远程调用专辑服务获取专辑分类信息
        CompletableFuture<Void> baseCategoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView baseCategoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            Assert.notNull(baseCategoryView, "分类:{}不存在！", albumInfo.getCategory3Id());
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolTaskExecutor);

        //远程调用专辑服务获取统计信息
        CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑统计信息, {}不存在", albumId);
            map.put("albumStatVo", albumStatVo);
        }, threadPoolTaskExecutor);


        //远程调用用户服务获取用户信息
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
            Assert.notNull(userInfoVo, "用户信息, {}不存在", albumInfo.getUserId());
            map.put("announcer", userInfoVo);
        }, threadPoolTaskExecutor);

        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                baseCategoryViewCompletableFuture,
                albumStatVoCompletableFuture,
                announcerCompletableFuture
        ).join();

        return map;
    }
}
